var Tool = {
    // 动画效果，封装
    requestAnimationFrame: function() {
        return window.requestAnimationFrame || //Chromium  
            window.webkitRequestAnimationFrame || //Webkit 
            window.mozRequestAnimationFrame || //Mozilla Geko 
            window.oRequestAnimationFrame || //Opera Presto 
            window.msRequestAnimationFrame || //IE Trident? 
            function(callback) { //Fallback function 
                window.setTimeout(callback, 1000 / 60);
            };
    },
    // 获取随机数
    rand: function(m, n) {
        return Math.floor(Math.random() * (n - m + 1) + m);
    },
    // 判断两个矩形是否重叠
    isOverlap: function(rc1, rc2) {
        if (rc1.x + rc1.width < rc2.x ||
            rc2.x + rc2.width < rc1.x ||
            rc1.y + rc1.height < rc2.y ||
            rc2.y + rc2.height < rc1.y
        ) {
            // 不碰撞
            return true;
        } else {
            return false;
        }
    },
    // 获取加载完成的图片对象数组
    getImage: function(src) {
        var img = new Image();
        img.onload = function() {
            // console.log('图片资源加载成功') 
        };
        img.onerror = function() { alert('图片加载失败：' + src) };
        img.src = src;
        return img;
    },
    drawRoundedRect: function(ctx, x, y, w, h, r, bdWidth, bdColor, bgcolor) {
        ctx.save();
        ctx.beginPath();
        ctx.moveTo(x + r, y);
        ctx.lineWidth = bdWidth;
        ctx.strokeStyle = bdColor;
        ctx.fillStyle = bgcolor;
        ctx.arcTo(x + w, y, x + w, y + h, r);
        ctx.arcTo(x + w, y + h, x, y + h, r);
        ctx.arcTo(x, y + h, x, y, r);
        ctx.arcTo(x, y, x + w, y, r);
        ctx.stroke();
        ctx.fill();
        ctx.closePath();
        ctx.restore();
    },
    isScope: function(coordinate, obj) {
        if (coordinate.x > obj.x && coordinate.x < obj.x + obj.width && coordinate.y > obj.y && coordinate.y < obj.y + obj.height) {
            return true;
        }
    }
}

// 获取canvas对象
var canvas = document.querySelector('#game');

// 获取绘图环境
var ctx = canvas.getContext('2d');